package com.zhonglz.support.listener;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.zhonglz.model.BuSignInfo;
import com.zhonglz.service.BuSignInfoService;
import com.zhonglz.service.SysBaseSetService;
import com.zhonglz.service.SysFileInfoService;
import com.zhonglz.support.event.SealSignDataEvent;
import com.zhonglz.support.model.SealSignData;
import com.zhonglz.util.Constans;
import com.zhonglz.util.result.ResultUtils;

/***
 * 签章状态监听
 * @author zhonglz
 *
 */
@Component
public class SignStateListener {

	private static Logger logger = LoggerFactory.getLogger(SignStateListener.class);
	
	@Autowired
	private SysFileInfoService sysFileInfoService;
	
	@Autowired
	private SysBaseSetService sysBaseSetService;
	
	@Autowired
	private BuSignInfoService buSignInfoService;
	
	@Order(50)
    @EventListener
    public void acceptEvent(SealSignDataEvent event) {
		logger.info("***************签章状态监听开始********************");
		SealSignData sealSignData =event.getSealSignData();
		// 文件表存储起来
		//存储的路径
		String minio_path = sysBaseSetService.baseSetValue(Constans.MINIO_PATH);
		String fileSuffix = ".pdf";
		String fileName = sealSignData.getSignBackFileUid()+fileSuffix;
		String pdfAddress = minio_path+"/"+fileName;
		int count = sysFileInfoService.uploadFileInfo(sealSignData.getSignBackFileUid(), fileName, pdfAddress,fileSuffix);
		if(count>0) {
			// 修改签署信息表状态
			BuSignInfo buSignInfo = sealSignData.getBuSignInfo();
			buSignInfo.setSignBackFileId(sealSignData.getSignBackFileUid());
			buSignInfo.setSignPage(sealSignData.getSignPage());
			buSignInfo.setPdfPageCount(sealSignData.getPdfPageCount());
			buSignInfo.setPdfWidth(sealSignData.getPdfWidth());
			buSignInfo.setPdfHeight(sealSignData.getPdfHeight());
			buSignInfo.setCoordX(sealSignData.getPageX());
			buSignInfo.setCoordY(sealSignData.getPageY());
			buSignInfo.setSignAlgorithm(sealSignData.getSignAlgorithm());
			buSignInfo.setSignTime(new Date());
			buSignInfoService.saveInfo(buSignInfo);
		}else {
			throw new RuntimeException("签署后文件入库失败");
		}
		logger.info("***************签章状态监听结束********************");
	}
}
